﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace FruitWeigher
{
	public partial class UISelected : UserControl
	{
		public UISelected()
		{
			// 为初始化变量所必需
			InitializeComponent();
            GSystem.UISelected = this;
            //B1.Click += new RoutedEventHandler(B_Click);
            //B2.Click += new RoutedEventHandler(B_Click);
            //B3.Click += new RoutedEventHandler(B_Click);
            //B4.Click += new RoutedEventHandler(B_Click);
            //B5.Click += new RoutedEventHandler(B_Click);
            int i = 1;
            foreach (var item in ItemList.Children)
            {
                var b = item as Button;
                if (b != null)
                {
                    b.Click += new RoutedEventHandler(B_Click);
                    var c = b.Content as UIItem0;
                    if (c != null)
                    {
                        c.Type = i;
                        i += 1;
                    }
                }

            }
            
		}

        void B_Click(object sender, RoutedEventArgs e)
        {
            GSystem.Root.AssertionSelected(((sender as Button).Content as UIItem0).Type);
        }
        public void UpDataCardList(List<ItemInfo> list)
        {
            HideItems();
            RandomCardList();
            for (int i = 0; i < list.Count; i++)
            {
                ShowOneItem(list[i].Type);
            }
        }
        public void RandomCardList()
        {
            for (int i = 0; i < ItemList.Children.Count; i++)
            {
                UIElement s = ItemList.Children[i];
                ItemList.Children.RemoveAt(i);
                ItemList.Children.Insert(GSystem.GamesRandom.Next() % ItemList.Children.Count, s);
            }
        }
        public void HideItems()
        {
            foreach (var item in ItemList.Children)
            {
                item.Visibility = Visibility.Collapsed;
            }
        }
        public void ShowOneItem(int type)
        {
            foreach (var item in ItemList.Children)
            {
                if (((item as Button).Content as UIItem0).Type == type)
                {
                    item.Visibility = Visibility.Visible;
                    break;
                }
            }
        }
	}
}